package com.approval.approvalapp.message;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.MyAppliaction;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.MessageListBean;
import com.approval.approvalapp.message.adapter.MessageListAdapter;
import com.approval.approvalapp.util.AppConfig;
import com.approval.approvalapp.util.DensityUtil;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseFragment;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.approval.approvalapp.util.view.CustomTouchSwipeRefreshLayout;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.CenterPopupView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * A simple {@link Fragment} subclass.
 */
public class MessageFragment extends BaseFragment {


    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.xrv)
    XRecyclerView xrv;
    @Bind(R.id.swipeRefreshLayout)
    CustomTouchSwipeRefreshLayout swipeRefreshLayout;
    @Bind(R.id.empty_ll)
    LinearLayout empty_ll;
    @Bind(R.id.type_tv)
    TextView state;
    @Bind(R.id.type_ll)
    LinearLayout type_ll;
    private MessageListAdapter messageListAdapter;
    private int pagenum = 1;
    private List<MessageListBean> messageListBeans = new ArrayList<>();
    private List<MessageListBean> messageListBeanshy = new ArrayList<>();
    private List<MessageListBean> messageListBeansyb = new ArrayList<>();
    public static MessageFragment instance;

    @Override
    protected int getLayoutId() {
        instance = this;
        return R.layout.fragment_message;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        instance = null;
    }

    @Override
    public void initView(View view) {
        xrv.setPullRefreshEnabled(false);
        xrv.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        messageListAdapter = new MessageListAdapter();
        xrv.setAdapter(messageListAdapter);


        swipeRefreshLayout.setRefreshing(true);
        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshData();
            }
        }, 500);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                xrv.setNoMore(false);
                swipeRefreshLayout.setRefreshing(true);
                refreshData();
            }
        });

        xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                swipeRefreshLayout.setRefreshing(false);
                pagenum = pagenum + 1;
                xrv.setNoMore(true);
            }
        });

        type_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new XPopup.Builder(MessageFragment.this.getActivity())
                        .autoOpenSoftInput(true)
                        .asCustom(new CustomPopup(MessageFragment.this.getActivity()))
                        .show();
            }
        });
    }

    @Override
    public void initData() {

    }

    public void refreshData() {
        state.setText("全部消息");

        pagenum = 1;
        JSONObject jsonObject = new JSONObject();
        //"20180001"
        jsonObject.put("org_code", SpUtil.getString(MyAppliaction.getInstance(), SpUtil.F_orgcode, "-1"));
        String parm = jsonObject.toString();
        NetRequsetUtil.getInstance().netRequestPostJson("v1/user/notice_list", parm, new NetDownResponse() {
            @Override
            public void success(String str) {
                Log.d("MessageFragmentss", str);
                messageListBeans = JSON.parseArray(str, MessageListBean.class);
                for (int i = 0; i < messageListBeans.size(); i++) {
                    if (messageListBeans.get(i).getTZTYPE() == 0){
                        //会议
                        messageListBeanshy.add(messageListBeans.get(i));
                    }else {
                        messageListBeansyb.add(messageListBeans.get(i));
                    }
                }
                messageListAdapter.setMessageListBeanList(messageListBeans);
                if (messageListBeans == null || messageListBeans.size() == 0){
                    empty_ll.setVisibility(View.VISIBLE);
                }else {
                    empty_ll.setVisibility(View.GONE);
                }
            }

            @Override
            public void errowithresponse(String str) {
                ErrorBeanUtil.getErrorMessage(str);
            }

            @Override
            public void erro() {
                ErrorBeanUtil.getErrorMessage(null);
            }

            @Override
            public void finish() {
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    public class CustomPopup extends CenterPopupView {
        public CustomPopup(@NonNull Context context) {
            super(context);
        }

        @Override
        protected int getImplLayoutId() {
            return R.layout.custom_popup_msg;
        }

        @Override
        protected void onCreate() {
            super.onCreate();
            final ImageView iv1 = findViewById(R.id.iv1);
            final ImageView iv2 = findViewById(R.id.iv2);
            final ImageView iv3 = findViewById(R.id.iv3);

            if (state.getText().toString().equals("全部消息")) {
                iv1.setVisibility(VISIBLE);
                iv2.setVisibility(INVISIBLE);
                iv3.setVisibility(INVISIBLE);
            } else if (state.getText().toString().equals("一般通知")) {
                iv1.setVisibility(INVISIBLE);
                iv2.setVisibility(VISIBLE);
                iv3.setVisibility(INVISIBLE);
            } else {
                iv1.setVisibility(INVISIBLE);
                iv2.setVisibility(INVISIBLE);
                iv3.setVisibility(VISIBLE);
            }


            findViewById(R.id.ll1).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    state.setText("全部消息");
                    dismiss();
                    messageListAdapter.setMessageListBeanList(messageListBeans);
                    if (messageListBeans == null || messageListBeans.size() == 0){
                        empty_ll.setVisibility(View.VISIBLE);
                    }else {
                        empty_ll.setVisibility(View.GONE);
                    }
                }
            });
            findViewById(R.id.ll2).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    state.setText("一般通知");
                    dismiss();
                    messageListAdapter.setMessageListBeanList(messageListBeansyb);
                    if (messageListBeansyb == null || messageListBeansyb.size() == 0){
                        empty_ll.setVisibility(View.VISIBLE);
                    }else {
                        empty_ll.setVisibility(View.GONE);
                    }
                }
            });
            findViewById(R.id.ll3).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    state.setText("会议通知");
                    dismiss();
                    messageListAdapter.setMessageListBeanList(messageListBeanshy);
                    if (messageListBeanshy == null || messageListBeanshy.size() == 0){
                        empty_ll.setVisibility(View.VISIBLE);
                    }else {
                        empty_ll.setVisibility(View.GONE);
                    }
                }
            });
        }

        @Override
        protected void onShow() {
            super.onShow();
        }

        @Override
        protected int getMaxWidth() {
            return DensityUtil.dip2px(MessageFragment.this.getActivity(), 280f);
        }

    }



}
